//
//  QSEnterpriseSignInController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/19.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSEnterpriseSignInController.h"
#import "QSSignInViewController.h"
#import "QSSignInRecordViewController.h"
#import "QSSingInInterface.h"
@interface QSEnterpriseSignInController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic)UICollectionView *collectionView;
@property (nonatomic, strong)NSArray *arrOfVC;
@property (nonatomic, strong)NSArray *imageArr;
/**签到Button*/
@property (nonatomic, strong)UIButton *signInButton;
/**签到记录Button*/
@property (nonatomic, strong)UIButton *recordButton;
@end

static NSString *cellID = @"DefaultCell";
@implementation QSEnterpriseSignInController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"驻企签到";
    
    [self configSubView];
    [self getCompanyList];//获取企业模型列表并配置子控制器
    self.view.backgroundColor = [UIColor QSColorWhiteFFFFFF];
}

- (void)configSubView{
    [self creatFooterButton];
    [self.view addSubview:self.collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(- kRealValue(69));
    }];
}
//配置子控制器、视图 绑定企业数组
- (void)configSubControllerWithCompanyList:(NSArray *)companyModelList{
    QSSignInViewController *signInVC = [QSSignInViewController new];
    QSSignInRecordViewController *recordVC = [QSSignInRecordViewController new];
    signInVC.companyModelList = companyModelList;
    recordVC.companyModelList = companyModelList;
    [self addChildViewController:signInVC];
    [self addChildViewController:recordVC];
    [self.collectionView addSubview:signInVC.view];
    [self.collectionView addSubview:recordVC.view];
    [signInVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.height.equalTo(_collectionView);
    }];
    [recordVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.equalTo(_collectionView);
        make.left.equalTo(signInVC.view.mas_right).offset(0);
    }];
}

- (void)creatFooterButton{
    [self.view addSubview:self.signInButton];
    [self.view addSubview:self.recordButton];
    [_signInButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(- kRealValue(10));
        make.width.offset(kScreenWidth / 2);
        make.height.offset(kRealValue(58));
    }];
    [_recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(- kRealValue(10));
        make.width.offset(kScreenWidth / 2);
        make.height.offset(kRealValue(58));
    }];
}

#pragma mark - Api - 获取绑定企业列表
- (void)getCompanyList{
//    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
//    [QSSingInInterface getCompanyListandBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSArray * _Nonnull result) {
//        [QSAppKeyWindow hideHud];
//        if (rspStatusAndMessage.code == kResponseSuccessCode) {
//        }
//    }];
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    //请求当前人员是否可显示全部
       [QSSingInInterface getShowAllCompanyAndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSString * _Nonnull result) {
           [QSAppKeyWindow hideHud];
           if (rspStatusAndMessage.code == kResponseSuccessCode) {
               //请求绑定的企业
               [QSSingInInterface getCompanyListWithPageIndex:1 EpAllCompany:result andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSBindingEnterprisesModel * _Nonnull result) {
                   if (rspStatusAndMessage.code == kResponseSuccessCode) {
                       [self configSubControllerWithCompanyList:result.list];//配置控制器与企业模型数组
                   }
               }];
           }
       }];
}
#pragma mark - 底部button触发
- (void)switchFooterItem:(UIButton *)sender{
   if (sender.selected) {
        if (sender.tag == 10) {
            _recordButton.selected = NO;
            [_recordButton setImage:[UIImage imageNamed:@"ic_signInrecord_unselect"] forState:UIControlStateNormal];
        }else{
            _signInButton.selected = NO;
            [_signInButton setImage:[UIImage imageNamed:@"ic_signIn_unselect"] forState:UIControlStateNormal];
        }
        
    }else{
        sender.selected = YES;
        if (sender.tag == 10) {
            //切换签到
            [self selectSignInButton];
        }else{
            //切换签到记录
            [self selectRecordButton];
            
        }
    }
}


//- (void)switchAction:(UIButton *)sender{
//    CGPoint offset = _collectionView.contentOffset;
//    if (sender.tag == 10)
//    {
//        CGFloat floa1 = 0;
//        CGFloat floa2 = _collectionView.contentOffset.y;
//        offset.x = floa1;
//        offset.y = floa2;
//        [_collectionView setContentOffset:offset animated:YES];
//        [self selectSignInButton];
//
//    }
//    else
//    {
//        CGFloat floa1 = kScreenWidth;
//        CGFloat floa2 = _collectionView.contentOffset.y;
//        offset.x = floa1;
//        offset.y = floa2;
//        [_collectionView setContentOffset:offset animated:YES];
//        [self selectRecordButton];
//    }
//}

//选中签到
- (void)selectSignInButton{
    
    [_signInButton setImage:[UIImage imageNamed:@"ic_signIn_select"] forState:UIControlStateNormal];
    [_recordButton setImage:[UIImage imageNamed:@"ic_signInrecord_unselect"] forState:UIControlStateNormal];
    [_signInButton setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateNormal];
    [_recordButton setTitleColor:[UIColor QSColorGrayCCCCCC] forState:UIControlStateNormal];
     _recordButton.selected = NO;
    CGPoint offset = _collectionView.contentOffset;
    CGFloat floa1 = 0;
    CGFloat floa2 = _collectionView.contentOffset.y;
    offset.x = floa1;
    offset.y = floa2;
    [_collectionView setContentOffset:offset animated:YES];
}
//选中签到记录
- (void)selectRecordButton{
    [_recordButton setImage:[UIImage imageNamed:@"ic_signInrecord_select"] forState:UIControlStateNormal];
    [_signInButton setImage:[UIImage imageNamed:@"ic_signIn_unselect"] forState:UIControlStateNormal];
    
    [_recordButton setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateNormal];
    [_signInButton setTitleColor:[UIColor QSColorGrayCCCCCC] forState:UIControlStateNormal];
    _signInButton.selected = NO;
    CGPoint offset = _collectionView.contentOffset;
    CGFloat floa1 = kScreenWidth;
    CGFloat floa2 = _collectionView.contentOffset.y;
    offset.x = floa1;
    offset.y = floa2;
    [_collectionView setContentOffset:offset animated:YES];
}

#pragma mark - 懒加载
- (UIButton *)signInButton{
    if (!_signInButton) {
        _signInButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _signInButton.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _signInButton.size = CGSizeMake(kScreenWidth / 2, kRealValue(58));
        [_signInButton setImage:[UIImage imageNamed:self.imageArr[0]] forState:UIControlStateNormal];
        [_signInButton setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateNormal];
        [_signInButton.titleLabel setFont:[UIFont QSFontOfSize14]];
        [_signInButton setTitle:@"签到" forState:UIControlStateNormal];
        _signInButton.selected = YES;
        _signInButton.tag = 10;
        [_signInButton setButtonTopImage:_signInButton];
        [_signInButton addTarget:self action:@selector(switchFooterItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _signInButton;
}

- (UIButton *)recordButton{
    if (!_recordButton) {
        _recordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _recordButton.size = CGSizeMake(kScreenWidth / 2, kRealValue(58));
        _recordButton.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        [_recordButton setImage:[UIImage imageNamed:self.imageArr[1]] forState:UIControlStateNormal];
        [_recordButton setTitleColor:[UIColor QSColorGrayCCCCCC] forState:UIControlStateNormal];
        [_recordButton.titleLabel setFont:[UIFont QSFontOfSize14]];
        [_recordButton setTitle:@"签到记录" forState:UIControlStateNormal];
        _recordButton.selected = NO;
        _recordButton.tag = 20;
        [_recordButton setButtonTopImage:_recordButton];
        [_recordButton addTarget:self action:@selector(switchFooterItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _recordButton;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - 110);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        [_collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    }
    return _collectionView;
}

- (NSArray *)imageArr{
    if (!_imageArr) {
        _imageArr = [NSArray arrayWithObjects:@"ic_signIn_select", @"ic_signInrecord_unselect", nil];
    }
    return _imageArr;
}
#pragma mark - CollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
   UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    return cell;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)selectIndex:(NSInteger)index{
    [self switchFooterItem:_recordButton];
}


- (void)dealloc{
    [self.collectionView
    removeObserver:self forKeyPath:@"contentOffset"];
}
@end
